package com.tsmti.doctor.dao.impl;

import com.tsmti.core.common.constant.Constants;
import com.tsmti.core.common.dao.impl.BaseDaoImpl;
import com.tsmti.core.common.query.data.Queryable;
import com.tsmti.core.util.BeanTransformerAdapter;
import com.tsmti.core.util.DateUtils;
import com.tsmti.core.util.StringUtils;
import com.tsmti.doctor.dao.PimsPathologyRequisitionDao;
import com.tsmti.doctor.dto.RequisitionView;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Package：com.tsmti.lab.dao.impl
 * @Class：PimsPathologyRequisitionDaoImpl
 * @Description： TODO
 * @Author：dcd
 * @Date：Created in 2018/11/13 11:20 AM
 * @Company:
 * @Version：
 * @Modified By:
 */
@Repository("pimsPathologyRequisitionDao")
public class PimsPathologyRequisitionDaoImpl extends BaseDaoImpl implements PimsPathologyRequisitionDao {
    @Override
    public int getInReceiveListCount(RequisitionView model) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT count(*)");
        sql.append(" FROM pims_pathology_requisition a,pims_req_material_nurse b,PIMS_REQ_FIELD_nurse c");
        sql.append(" WHERE a.id = b.requisitionid(+) and a.id = c.requisitionid(+)");
        getInReceiveSql(model, sql);
        return countSqlTotal(sql.toString());
    }

    @Override
    public List<RequisitionView> getInReceiveList(RequisitionView model, Queryable queryable) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT a.id,TO_CHAR(a.reqreceivetime,'yyyy-MM-dd hh24:mi:ss') reqreceivetime,TO_CHAR(a.reqdate,'yyyy-MM-dd hh24:mi:ss') reqdate,a.requisitionno, a.reqpatientname, a.reqpatientnumber,a.reqpatienttype,a.reqwardname, to_char(a.reqpatientage) reqpatientage,");
        sql.append(" to_char(a.reqpatientsex) reqpatientsex, a.reqitemnames,a.reqsendmaterial, a.reqsampleid, to_char(a.reqreceiveorgid) reqreceiveorgid,a.reqdeptname, a.reqdoctorname,to_char(a.reqstate) reqstate,a.reqBedNum,a.reqSign,");
        sql.append(" (case a.pathologyid when 1 then  COUNT(b.id)   else   COUNT(c.id) end) samplenum");
        sql.append(" FROM pims_pathology_requisition a,pims_req_material_nurse b,PIMS_REQ_FIELD_nurse c");
        sql.append(" WHERE a.id = b.requisitionid(+) and a.id = c.requisitionid(+)");
        getInReceiveSql(model, sql);
        sql.append(" GROUP BY a.id,reqdate,a.reqreceivetime, a.requisitionno,a.reqpatientname, a.reqpatientnumber,a.reqpatienttype, a.reqwardname,");
        sql.append(" a.reqpatientage,a.reqpatientsex,a.reqitemnames,a.reqsendmaterial,a.reqsampleid, a.reqreceiveorgid, a.reqdeptname, a.reqdoctorname, a.reqstate, a.pathologyid,a.reqBedNum,a.reqSign");
        sql = new StringBuffer(sortByQueryable(sql.toString(), queryable));
        Query query = getSession().createSQLQuery(sql.toString()).setResultTransformer((new BeanTransformerAdapter(RequisitionView.class)));
        if (queryable != null) {
            query.setFirstResult(getStartPageNum(queryable));
            query.setMaxResults(getEndPageNum(queryable));
        }
        return query.list();
    }

    private void getInReceiveSql(RequisitionView model, StringBuffer sql) {
        if (StringUtils.isNotEmpty(model.getReqReceiveTime())) {
            if (model.getReqReceiveTime().indexOf(Constants.COMMA) == -1) {
                sql.append(" and TO_CHAR(a.reqReceiveTime,'yyyy-MM-dd hh24:mi:ss') <= '" + model.getReqReceiveTime() + "'");
            } else {
                String start = DateUtils.getStartTme(model.getReqReceiveTime());
                String end = DateUtils.getEndTme(model.getReqReceiveTime());
                if (StringUtils.isNotEmpty(start)) {
                    sql.append(" and TO_CHAR(a.reqReceiveTime,'yyyy-MM-dd hh24:mi:ss') >= '" + start + "'");
                }
                if (StringUtils.isNotEmpty(end)) {
                    sql.append(" and TO_CHAR(a.reqReceiveTime,'yyyy-MM-dd hh24:mi:ss') <= '" + end + "'");
                }
            }
        }
        if (StringUtils.isNotEmpty(model.getReqDate())) {
            if (model.getReqDate().indexOf(Constants.COMMA) == -1) {
                sql.append(" and TO_CHAR(a.reqdate,'yyyy-MM-dd hh24:mi:ss') <= '" + model.getReqDate() + "'");
            } else {
                String start = DateUtils.getStartTme(model.getReqDate());
                String end = DateUtils.getEndTme(model.getReqDate());
                if (StringUtils.isNotEmpty(start)) {
                    sql.append(" and TO_CHAR(a.reqdate,'yyyy-MM-dd hh24:mi:ss') >= '" + start + "'");
                }
                if (StringUtils.isNotEmpty(end)) {
                    sql.append(" and TO_CHAR(a.reqdate,'yyyy-MM-dd hh24:mi:ss') <= '" + end + "'");
                }
            }
        }
        if (StringUtils.isNotEmpty(model.getReqState())) {
            if (model.getReqState().indexOf(Constants.GT) > -1) {
                sql.append(" and a.reqstate > " + model.getReqState().substring(0, 1));
            } else {
                sql.append(" and a.reqstate = " + model.getReqState());
            }
        }
        if (StringUtils.isNotEmpty(model.getReqPatientName())) {
            sql.append(" and a.reqpatientname = '" + model.getReqPatientName() + "'");
        }
        if (StringUtils.isNotEmpty(model.getRequisitionNo())) {
            sql.append(" and a.requisitionno = '" + model.getRequisitionNo() + "'");
        }
    }
}
